﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MYNAMESPACE;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace final_project
{
    public partial class CreateAccount : Form
    {
        private connect_db con;
        public CreateAccount()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 
            
            
            if (ID.Text.Length == 0 || Name_textBox.Text.Length == 0 || Password_textBox.Text.Length == 0 || Address_textBox.Text.Length == 0 || Mail_textBox.Text.Length == 0 || Phone_textBox.Text.Length == 0 || Balance_textBox.Text.Length == 0)
            {

                MessageBox.Show("must have value");


            }
            else
            {
                user US = new user();
                US.setnational_id(Int32.Parse(ID.Text));
                US.setname(string.Format(Name_textBox.Text));
                US.setpassword(string.Format(Password_textBox.Text));
                US.setaddress(string.Format(Address_textBox.Text));
                US.setemail(string.Format(Mail_textBox.Text));
                US.setphone(long.Parse(textBox1.Text + Phone_textBox.Text));
                US.setgender(string.Format(Gender_textBox.Text));
                US.setage(Int32.Parse(Age_textBox.Text));
                US.setbalance(double.Parse(Balance_textBox.Text));



                con = new connect_db();
                con.sqlquery("select id from userdata where id=(@namep)");
                con.cmd.Parameters.Add("@namep", US.getnational_id());
                int count = 0;
                foreach (DataRow dr in con.QueryEx().Rows)
                {

                    count = count + 1;


                }
                if (count == 0)
                {

                    if (US.getbalance() != 0)
                    {
                        if (US.getage() != 0)
                        {
                            con.sqlquery("INSERT INTO userdata (id, name , pass , useradd , mail , phone, gender ,age , balance,Co_id) VALUES('" + US.getnational_id() + "','" + US.getname() + "','" + US.getpassword() + "','" + US.getaddress() + "','" + US.getmail() + "','" + US.getphone() + "','" + US.getgender() + "','" + US.getage() + "','" + US.getbalance() + "','" + textBox1.Text + "')");
                            con.sqlnonex();
                            MessageBox.Show("Done");
                            this.Hide();
                            User f1 = new User();
                            f1.ShowDialog();
                        }
                        else
                        {
                            MessageBox.Show(" ERROR your age less than 21 ");

                        }
                    }
                    else
                    {

                        MessageBox.Show(" your balance must start with 1000 LE or more ");
                    }
                }
                else
                {

                    MessageBox.Show("ID is Exist");
                }
            }
        }

        private void National_ID_TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            char cb = e.KeyChar;
            if (!char.IsDigit(cb) && cb != 8)
            {

                e.Handled = true;

            }
        }

        private void Balance_textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            char cb = e.KeyChar;
            if (!char.IsDigit(cb) && cb != 8)
            {

                e.Handled = true;

            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            User f1 = new User();
            f1.ShowDialog();
        }

        private void Phone_textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            char cb = e.KeyChar;
            if (!char.IsDigit(cb) && cb != 8)
            {

                e.Handled = true;

            }
        }

        private void Age_textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            char cb = e.KeyChar;
            if (!char.IsDigit(cb) && cb != 8)
            {

                e.Handled = true;

            }
        }

        private void CreateAccount_Load(object sender, EventArgs e)
        {

        }

        private void ID_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(ID.Text))
            {

                e.Cancel = true;
                ID.Focus();
                errorProvider1.SetError(ID, "ID Must Be Entered !");

            }

            else if (!Regex.IsMatch(ID.Text, @"^\d{14}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(ID, "Valid ID 14 Number");

            }



            else if (Regex.IsMatch(ID.Text, @"^\d{14}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(ID, "");

            }
            else { }

        }

        private void Name_textBox_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Name_textBox.Text))
            {

                e.Cancel = true;
                Name_textBox.Focus();
                errorProvider1.SetError(Name_textBox, "Name Must Be Entered !");

            }
            else if (Regex.IsMatch(Name_textBox.Text, @"^[a-zA-Z''-'\s]{1,40}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(Name_textBox, "");

            }
            else
            {

            }

        }

    

        private void Address_textBox_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(Address_textBox.Text))
            {

                e.Cancel = true;
                Address_textBox.Focus();
                errorProvider1.SetError(Address_textBox, "Address  Must Be Entered !");

            }
            else if (Regex.IsMatch(Address_textBox.Text, @"^[a-zA-Z''-'\s]{1,40}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(Address_textBox, "");

            }
            else
            {

            }

        }

        private void Mail_textBox_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Mail_textBox.Text))
            {

                e.Cancel = true;
                Mail_textBox.Focus();
                errorProvider1.SetError(Mail_textBox, "Mail Must Be Entered !");

            }
            else if (!Regex.IsMatch(Mail_textBox.Text, @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Mail_textBox, "Address EX someone@example.com !");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Mail_textBox, "");
            }

        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(textBox1.Text))
            {

                e.Cancel = true;
                textBox1.Focus();
                errorProvider1.SetError(textBox1, "Region Code  Must Be Entered !");

            }
            else if (!Regex.IsMatch(textBox1.Text, @"^\d{3}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(textBox1, "Enter 3 Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(textBox1, "");
            }

        }

        private void Phone_textBox_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Phone_textBox.Text))
            {

                e.Cancel = true;
                Password_textBox.Focus();
                errorProvider1.SetError(Phone_textBox, "Phoe Must Be Entered !");

            }
            else if (!Regex.IsMatch(Phone_textBox.Text, @"^\d{8}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Phone_textBox, "Enter 8 Numbers !");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Phone_textBox, "");
            }


        }

        private void Gender_textBox_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Gender_textBox.Text))
            {

                e.Cancel = true;
                Gender_textBox.Focus();
                errorProvider1.SetError(Gender_textBox, "Gender Must Be Entered !");

            }
            else if (!Regex.IsMatch(Gender_textBox.Text, @"^[a-zA-Z]{1,5}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Gender_textBox, "Invalid !");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Gender_textBox, "");

            }


        }

        private void Age_textBox_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Age_textBox.Text))
            {

                e.Cancel = true;
                Age_textBox.Focus();
                errorProvider1.SetError(Age_textBox, "Age Must Be Entered !");

            }
            else if (!Regex.IsMatch(Age_textBox.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Age_textBox, "No Negative Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Age_textBox, "");
            }

        }

        private void Balance_textBox_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Balance_textBox.Text))
            {

                e.Cancel = true;
                Balance_textBox.Focus();
                errorProvider1.SetError(Balance_textBox, "Balance Must Be Entered !");

            }
            else if (!Regex.IsMatch(Balance_textBox.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Balance_textBox, "Invali Input !");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Balance_textBox, "");
            }

        }

        private void Password_textBox_Validating_1(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Password_textBox.Text))
            {

                e.Cancel = true;
                Password_textBox.Focus();
                errorProvider1.SetError(Password_textBox, "Password Must Be Entered !");
                //passworderror.SetError(Password_textBox.Text, "Password required!");
            }
            else if (!Regex.IsMatch(Password_textBox.Text, @"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Password_textBox, "Password Must more Than 8 Digits Contain upper-lowercase,numbers and special characters");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Password_textBox, "");
            }

        }

        
    }
}
